<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" class="ele-form-search" label-width="77px" @keyup.enter.native="reload" @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="产品名称:">
              <el-input v-model="where.product_name" clearable placeholder="请输入关联产品名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:qr_code:index')" class="ele-btn-icon" icon="el-icon-search" type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where" height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button v-if="permission.includes('sys:qr_code:add')" class="ele-btn-icon" icon="el-icon-plus" size="small" type="primary" @click="toAddCode()">
            批量生成二维码
          </el-button>
          <el-button v-if="permission.includes('sys:bom:add')" class="ele-btn-icon" icon="el-icon-plus" size="small" type="primary" @click="exportAll(null)">
            批量导出
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link v-show="row.status_code === 'associated'" :underline="false" icon="el-icon-edit" type="primary" @click="showQrcode(row.url, row.id)">
            预览
          </el-link>
          <el-link v-show="row.status_code === 'un_associated'" :underline="false" icon="el-icon-edit" type="primary" @click="openEdit(row)">
            关联产品
          </el-link>
          <el-popconfirm class="ele-action" title="确定取消关联？" @confirm="openEdit(row)">
            <el-link v-show="row.status_code === 'associated'" slot="reference" :underline="false" icon="el-icon-edit" type="primary">
              取消关联
            </el-link>
          </el-popconfirm>
          <el-popconfirm class="ele-action" title="确定要删除此二维码吗？" @confirm="remove(row)">
            <el-link v-if="permission.includes('sys:qr_code:delete')" slot="reference" :underline="false" icon="el-icon-delete" type="danger">
              删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <AddCode :visible.sync="flag" @changeFlag="changeFlag"></AddCode>
    <EditCode ref="CodeEdit" :data="current" :visible.sync="showEdit" @editCode="editCode" />
    <show-qrcode ref="showQrcode" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ShowQrcode from './components/show-qrcode.vue'
import EditCode from './components/relevance-code.vue'
import AddCode from './components/add-code.vue'
import { download } from "@/utils/download";

export default {
  name: 'SystemLevel',
  components: {
    ShowQrcode,
    EditCode,
    AddCode
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      flag: false,
      // 表格数据接口
      url: '/qr_code',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'code',
          label: '二维码编号',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'status_title',
          label: '二维码状态',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'product.name',
          label: '关联产品',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'product.product_number',
          label: '产品编号',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          prop: 'create_time_format',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 240,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  methods: {
    changeFlag() {
      this.flag = false
      this.reload()
    },
    toAddCode() {
      this.flag = true
    },

    //批量导出
    exportAll() {
      let temp = (this.selection.map(item => {
        return item.id
      })).join(',')
      if (temp != '') {
        const loading = this.$loading({ lock: true });
        this.$http.get(`/qr_code_export?ids=${temp}`).then(res => {
          loading.close();
          if (res.data.code === 0) {
            download(res.data.data.url)
            this.$message.success('导出成功')
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      } else {
        return this.$message.error('请选择二维码');
      }

    },
    // 编辑
    editCode(value, id) {
      const loading = this.$loading({ lock: true });
      this.$http.put('/qr_code/' + id, value).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.$refs.CodeEdit.updateVisible(false);
          this.reload();
        } else {
          loading.close();
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    // 显示二维码
    showQrcode(url, id) {
      this.$refs.showQrcode.dialogVisible = true;
      this.$refs.showQrcode.url = url;
      this.$refs.showQrcode.id = id;
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      if (row ? row.status_code === 'associated' : null) {
        const loading = this.$loading({ lock: true });
        this.$http.put('/qr_code/' + row.id, { product_id: 0 }).then(res => {
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.$refs.CodeEdit.updateVisible(false);
            this.$refs.CodeEdit.code_name = "";
            this.reload();
            loading.close();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
        return;
      } else if (row ? row.status_code === 'un_associated' : null) {
        this.$refs.CodeEdit.isUpdate = true;
        this.current = row;
        this.showEdit = true;
      } else {
        this.current = row;
        this.showEdit = true;
      }
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.delete('/qr_code/' + row.id).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>
